package com.yz.jdd.module.cashier.scanpay;

import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.yz.jdd.R;
import com.yz.jdd.base.event.Events;
import com.yz.jdd.base.event.RxBus;
import com.yz.jdd.base.utils.BaseConfigs;
import com.yz.jdd.base.utils.PayConfig;
import com.yz.jdd.base.utils.PreferencesUtils;
import com.yz.jdd.network.request.Request_Pay;


/**
 * Created by zrck on 2017/2/27.
 */

public class ScanPaymentFragment extends Fragment {
    //    private TextView amountType;
    private TextView tv_totalAmount;
    private ImageView iv_qrcode;//收款码
    private ImageView shadow_img;// 阴影效果
    private ImageView refresh_img;// 阴影效果
    private String totalAmount = "";
    private TextView tv_refresh;//重试刷新
    private TextView tv_paytype;
    private TextView txt_shopname;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Request_Pay request_pay = (Request_Pay) getActivity().getIntent().getSerializableExtra(PayConfig.INTENT_ORDER_FROM);
        if (request_pay != null) {
            if (PayConfig.TYPE_ORDER_RECHARGE.equals(request_pay.getOrderType())) {
                totalAmount = request_pay.getRechargeAmount();
            } else {
                totalAmount = request_pay.getTotalAmount();
            }

        }

    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_payment, container, false);
//        LinearLayout contentView = (LinearLayout) view.findViewById(R.id.payment_content_view);
//        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
//        lp.setMargins(0, Tools.getStatusBarHeight(getActivity()) , 0, 0); //+ Tools.dip2px(getActivity(), 98)
//        contentView.setLayoutParams(lp);
        tv_refresh = (TextView) view.findViewById(R.id.tv_paycode_refresh);
        tv_refresh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RxBus.getInstance().send(Events.EVENT_INIT_QRCODEVIEW, Events.EVENT_INIT_QRCODEVIEW);
            }
        });
        tv_paytype = (TextView) view.findViewById(R.id.tv_scanpay_paytype);
        txt_shopname = (TextView) view.findViewById(R.id.txt_shopname);
        String shopName = PreferencesUtils.getString(getActivity(), BaseConfigs.PREF_SHOPNAME, "");
        if (shopName.length() > 16) {
            shopName = shopName.substring(0, 16)+"...";
        }
        txt_shopname.setText(shopName);

//        amountType = (TextView) view.findViewById(R.id.payresult_amount_type);
//        amountType.setVisibility(View.GONE);
        tv_totalAmount = (TextView) view.findViewById(R.id.payresult_amount);
        tv_totalAmount.setText(totalAmount);
        iv_qrcode = (ImageView) view.findViewById(R.id.iv_pay_qrcode);
        shadow_img = (ImageView) view.findViewById(R.id.shadow_img);
        refresh_img = (ImageView) view.findViewById(R.id.refresh_img);
        refresh_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RxBus.getInstance().send(Events.EVENT_INIT_QRCODEVIEW, Events.EVENT_INIT_QRCODEVIEW);
            }
        });
        shadow_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RxBus.getInstance().send(Events.EVENT_INIT_QRCODEVIEW, Events.EVENT_INIT_QRCODEVIEW);
            }
        });

        return view;
    }

    public ImageView getQrcodeView() {
        if (iv_qrcode != null) {
            return iv_qrcode;
        }
        return null;
    }

    public ImageView getShadowView() {
        if (shadow_img != null) {
            return shadow_img;
        }
        return null;
    }
    public ImageView getRefreshIMG() {
        if (refresh_img != null) {
            return refresh_img;
        }
        return null;
    }

    public TextView getRefreshView() {
        if (tv_refresh != null) {
            return tv_refresh;
        }
        return null;
    }

    public TextView getPayType() {
        if (tv_paytype != null) {
            return tv_paytype;
        }
        return null;
    }

    public View setPayTypeTextIsAli(boolean boo) {
        if (tv_paytype != null) {
            if (boo) {
                tv_paytype.setText("请使用支付宝支付");
            } else {
                tv_paytype.setText("请使用微信支付");
            }
        }
        return null;
    }
}